/**
 * 起重器械项目设备表craneProject.js
 * 版本号： v1.0.0
 */
$(function(){
    var lastChecked = null;
    $("#craneProject_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        striped: true,
        border: false,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/craneProject/craneProjectList.do',
        idField: 'cpId',
        frozenColumns:[[
            {field:'cpId',checkbox:true}
        ]],
        columns: [
            [
                {field: 'cpCode', title: '起重器械序列号/设备编号', width: 200, fixed: true},
                {field: 'project', title: '起重机械所属项目', width: 200, fixed: true,formatter:function(val){
                    if(val){
                        return val.prName;
                    }
                    }},
                {field: 'distributLoc', title: '布点位置', width: 230, fixed: true},
                {field: 'deviceId', title: '设备序列号', width: 180, fixed: true},
                {field: 'meIpPortInfo', title: '设备IP与端口', width: 120, fixed: true},
                {field: 'meStatusCommonDict', title: '设备状态', width: 70, fixed: true, formatter:function(val){
                            return val.propertyName;
                    }},
                {field: 'onLineStatusCommonDict', title: '在线状态', width: 70, formatter:function(val){
                            if (val.propertyCode == 1) return "<span style='color: green; font-weight: bold'>在线</span>";
                            if (val.propertyCode == 2) return "<span style='color: red; font-weight: bold'>离线</span>";
                    }}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#craneProject_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#craneProject_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                lastChecked = null;
                $("#craneProject_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#craneProject_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});


/**
 * 删除起重器械项目设备表
 */
function craneProject_del() {
    var row = $("#craneProject_dataList").datagrid("getSelected");
    var checked = $("#craneProject_dataList").datagrid("getChecked");
    if (!checked || !checked.length) {
        $.messager.show({ title: '操作提示', msg: "请选择删除起重器械项目设备表", timeout: 3000, showType: "slide" });
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.cpName + ' 起重器械项目设备表吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/craneProject/del.do?id=" + row.id,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#craneProject_dataList").datagrid("reload", {
                        cpCode:$("#cpCode_").val(),
                        meStatus:$("#meStatus_").combobox("getValue"),
                        cpUseType:$("#cpUseType_").combobox("getValue")
                    });
                }
            })
        }
    });
}

//查询起重器械项目设备表
function craneProject_findList(){
    $("#craneProject_dataList").datagrid("reload",{cpName:$("#cpName_").val(),
        cpCode:$("#cpCode_").val(),
        meStatus:$("#meStatus_").combobox("getValue"),
        cpUseType:$("#cpUseType_").combobox("getValue")
    });
    //清楚选中项
    $("#craneProject_dataList").datagrid("clearSelections");
}

function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            window.location.href = CONTEXT_PATH + "/craneProject/toCraneProject.do";
                        }
                    });
                }
                //删除按钮
                if("delBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            craneProject_del();
                        }
                    });
                }
                //修改按钮
                if("editBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            craneProject_edit();
                        }
                    });
                }
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            craneProject_findList();
                        }
                    });
                }
                //查看详细按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            craneProject_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}


function craneProject_edit() {
    var row =$("#craneProject_dataList").datagrid("getSelected");
    var checked = $("#craneProject_dataList").datagrid("getChecked");
    if (!checked || !checked.length) {
        $.messager.show({ title: '操作提示', msg: "请选择删除起重器械项目设备表", timeout: 3000, showType: "slide" });
        return;
    }
    window.location.href = CONTEXT_PATH + "/craneProject/toCraneProject.do?cpId="+row.cpId;
}

/***
 * 动态刷新页面
 * @param prId
 */
function refreshPageNumber(cpId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + cpId).datagrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + cpId).datagrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}

/**
 * 查看起重机详情
 */
function craneProject_view(iconCls){
    var row = $("#craneProject_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的起重机详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(qzjxq.id, qzjxq.url + row.cpId, iconCls, qzjxq.closable, qzjxq.title);
}